package com.xjoyt.pay.controllers.seabluesky;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.xjoyt.pay.dto.OrderUrlResult;
import com.xjoyt.pay.dto.params.OrderAdapterParam;
import com.xjoyt.pay.payutil.HttpUtil;
import com.xjoyt.pay.payutil.MD5;
import com.xjoyt.pay.payutil.MapUtils;

/**
 * demo适配器
 * 
 * @author Administrator
 *
 */
@RestController
@RequestMapping("seaBlueSky/proxyPay")
public class ProxyPayController {
	public static String payUrl = "http://121.196.196.167:8889/tran/cashier/TX0001.ac";// 服务器地址
	public static String queryUrl = "http://121.196.196.167:8889/tran/cashier/TX0002.ac";

	/**
	 * 向上游发起代付请求
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("/proxPay")
	public OrderUrlResult proxPay(String no,String keyp,String orgNop, @RequestBody OrderAdapterParam param) {
		OrderUrlResult orderUrlResult = new OrderUrlResult();

		String appId = "17082200001868";// 商户编号 商户参数
		String key = "EF8217135E0D7299FCBC708370667AFC";// 秘钥 商户参数
		String orgNo = "8170800243";// 商户参数
		String out_order_no = param.order.no;// 订单号
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("version", "2.1");
			paramMap.put("orgNo", orgNo); // 机构号
			paramMap.put("custId", appId); // 商户号
			paramMap.put("custOrdNo", out_order_no);
			paramMap.put("casType", "00");
			paramMap.put("casAmt", param.param.money + "");// 分
			paramMap.put("accountName", param.merchantChannel.merchant.cardName);
			paramMap.put("cardNo", param.merchantChannel.merchant.cardNo);
			paramMap.put("cnapsCode", param.merchantChannel.merchant.bankNo);
			String sign = sign(paramMap, key);
			paramMap.put("sign", sign);

			String resStr = HttpUtil.httpPost(payUrl, paramMap);
			JSONObject resultMap = JSONObject.parseObject(resStr);
			// 订单状态 00:未处理 01:待结算 02:-- 03:可疑 04:处理中 05:关闭 06:清算中 07:清算完成
			// 08:清算失败
			String ordStatus = (String) resultMap.getString("ordStatus");
			// TODO 自行处理
		} catch (Exception e) {
			e.printStackTrace();
		}

		return orderUrlResult;
	}

	/**
	 * 查询代付状态
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("/proxPayStatus")
	public OrderUrlResult proxPayStatus(@RequestBody OrderAdapterParam param) {
		OrderUrlResult orderUrlResult = new OrderUrlResult();

		String orderNo = param.param.outTradeNo;// 支付订单号
		String appId = "";// 上游通道ID 商户参数
		String appKey = "";// 上游通道秘钥 商户参数
		String orgNo = "";// 商户参数

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("version", "2.1");
		paramMap.put("orgNo", orgNo);
		paramMap.put("custId", appId);
		paramMap.put("custOrdNo", orderNo);
		paramMap.put("sign", sign(paramMap, appKey));
		try {
			String resStr = HttpUtil.httpPost(queryUrl, paramMap);
			JSONObject resultMap = JSONObject.parseObject(resStr);
			// 订单状态 00:未处理 01:待结算 02:-- 03:可疑 04:处理中 05:关闭 06:清算中 07:清算完成
			// 08:清算失败
			String ordStatus = (String) resultMap.getString("ordStatus");
			// TODO 自行处理
		} catch (Exception e) {
			e.printStackTrace();
		}

		return orderUrlResult;
	}

	public static String sign(Map<String, Object> paramMap, String signKey) {
		String signStr = MapUtils.getSignStrByTreeMap(paramMap, true);
		System.out.println("签名元数据:[{}]" + signStr);
		signStr += "&key=" + signKey;
		String signValue = MD5.sign(signStr, "UTF-8").toUpperCase();
		System.out.println("签名前完整字符串:[{}] 签名结果[{}]" + signStr + "------" + signValue);
		return signValue;
	}

}
